package com.sh.wy.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.util.Result;
import com.sh.wy.system.entity.CheckMenuTreeVo;
import com.sh.wy.system.entity.SaveMenuIdsByRoleIdParam;
import com.sh.wy.system.entity.SysRoleParam;
import com.sh.wy.system.service.ISysMenuService;
import com.sh.wy.system.service.ISysRoleService;
import com.sh.wy.system.entity.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 世豪学Java
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/api/role")
@Api(tags = "用户角色管理")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService menuService;

    //查询用户角色
    @GetMapping("/list")
    @ApiOperation("角色列表")
    public Result list(SysRoleParam sysRoleParam){
        IPage<SysRole> page = sysRoleService.list(sysRoleParam);
        return Result.success(page);
    }

    //添加用户角色
    @ApiOperation("添加角色")
    @PostMapping
    public Result addRole(@RequestBody SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if (save){
            return Result.success();
        }
        return Result.error(500,"添加失败");
    }

    //修改角色
    @ApiOperation("修改角色")
    @PutMapping
    public Result editRole(@RequestBody SysRole sysRole){
        boolean update = sysRoleService.updateById(sysRole);
        if(update){
            return Result.success();
        }
        return Result.error(500,"修改失败");
    }

    //删除角色
    @ApiOperation("删除角色")
    @DeleteMapping("/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Integer roleId){
        boolean deleted = sysRoleService.removeById(roleId);
        if(deleted){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }

    @ApiOperation("获得分配权限菜单")
    @GetMapping("/getAssignTree/{roleId}")
    public Result getAssignTree(@PathVariable Integer roleId){
        CheckMenuTreeVo checkMenuTreeVo = menuService.getAssignTree(roleId);
        return Result.success(checkMenuTreeVo);
    }

    @ApiOperation("保存角色拥有的权限信息")
    @PutMapping("/saveMenuIdsByRoleId")
    public Result saveMenuIdsByRoleId(@RequestBody SaveMenuIdsByRoleIdParam param){
        menuService.saveMenuIdsByRoleId(param);
        return Result.success();
    }

    @ApiOperation("获取所有角色列表")
    @GetMapping("/getList")
    public Result getList(){
        List<SysRole> list = sysRoleService.list();
        return Result.success(list);
    }
}
